package cn.boy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.boy.dao.AdminstratorDao;
import cn.boy.domain.Adminstrator;
import cn.boy.utils.JdbcUtils;

import com.google.gson.Gson;

public class AdminstratorDaoImpl implements AdminstratorDao {
	
	public void add(Adminstrator admin){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = JdbcUtils.getConnection();
			String sql ="insert into adminstrator(" +
					"NO,name,password,sex,age,department) values (?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, admin.getNO());
			ps.setString(2, admin.getName());
			ps.setString(3, admin.getPassword());
			ps.setString(4, admin.getSex());
			ps.setInt(5, Integer.parseInt(admin.getAge()));
			ps.setString(6, admin.getDepartment());
			System.out.println(ps);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();	
			throw new RuntimeException(e);  
		}finally{	
			JdbcUtils.free(rs, ps, conn);
		}
		
	}
	
	public void delete(String no){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql ="delete from adminstrator where NO = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, no);
			System.out.println(ps);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
	}
	
	public void update(Adminstrator admin){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql ="update adminstrator set name = ? ," +
					" sex = ? ,age = ?, department = ?" +
					" where NO = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, admin.getName());
			ps.setString(2, admin.getSex());
			ps.setInt(3,Integer.parseInt(admin.getAge()));
			String department = admin.getDepartment();
			if(department!=null && !department.equals("")){
				ps.setString(4, admin.getDepartment());
			}
			ps.setString(5, admin.getNO());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
	}
	
	/**
	 * 修改密码
	 * @param no
	 * @param password
	 */
	public void update(String no,String password) {

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql ="update adminstrator set password = ? where NO = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, password);
			ps.setString(2, no);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
		
	}
	
	
	public String queryAll(){

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Adminstrator> list = new ArrayList<Adminstrator>();
		try {
			conn = JdbcUtils.getConnection();
			String sql ="select * from adminstrator";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if(!rs.next()){
				return "";
			}
			rs.beforeFirst();
			while(rs.next()){
				Adminstrator admin = mappingAdminstrator(rs);
				list.add(admin);
			}
			Gson gson = new Gson();
			return gson.toJson(list);
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
	
	}
	/**
	 * 根据部门查询管理员,支持模糊查询
	 * @param dormitoryNO
	 * @return
	 */
	public String queryAdminstratorsByDepartment(String department){

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Adminstrator> list = new ArrayList<Adminstrator>();
		try {
			conn = JdbcUtils.getConnection();
			String sql ="select * from adminstrator where department like ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "%"+department+"%");
			rs = ps.executeQuery();
			if(!rs.next()){
				return "";
			}
			rs.beforeFirst();
			while(rs.next()){
				Adminstrator admin = mappingAdminstrator(rs);
				list.add(admin);
			}
			Gson gson = new Gson();
			return gson.toJson(list);
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
	}
	
	public String query(String no){

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql ="select * from adminstrator where NO = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, no);
			rs = ps.executeQuery();
			if(rs.next()){
				Adminstrator s = mappingAdminstrator(rs);
				Gson gson = new Gson();
				return gson.toJson(s);
			}
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
		return "";
	
	}
	
	public String query(String no,String password){

		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql ="select * from adminstrator where NO = ? and password = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, no);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if(rs.next()){
				Adminstrator admin = mappingAdminstrator(rs);
				Gson gson = new Gson();
				return gson.toJson(admin);
			}
		} catch (SQLException e) {
			e.printStackTrace();	//调试完后删除
			throw new RuntimeException(e);  //抛出一个运行时异常
		}finally{	//释放资源
			JdbcUtils.free(rs, ps, conn);
		}
		return "";
	
	}

	private Adminstrator mappingAdminstrator(ResultSet rs) throws SQLException {
		Adminstrator admin = new Adminstrator();
		admin.setNO(rs.getString("NO"));
		admin.setName(rs.getString("name"));
		admin.setSex(rs.getString("sex"));
		admin.setAge(rs.getInt("age")+"");
		if(rs.getString("department")!=null && !rs.getString("department").equals("")){
			admin.setDepartment(rs.getString("department"));
		}
		

		return admin;
	}

}
